package com.example.jetpackpaging.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.example.jetpackpaging.databinding.MovieItemBinding
import com.example.jetpackpaging.model.Repo

class RepoAdapter(private val context:Context): PagingDataAdapter<Repo,BindingViewHolder>(object: DiffUtil.ItemCallback<Repo>(){
    //areItemsTheSame和areContentsTheSame这两个方法都返回true，则认为是同一个元素，则刷新时候就不会重新绘制
    override fun areItemsTheSame(oldItem: Repo, newItem: Repo): Boolean {
        return oldItem.id == newItem.id
    }
    override fun areContentsTheSame(oldItem: Repo, newItem: Repo): Boolean {
         return oldItem == newItem
    }
    }) {

    override fun onBindViewHolder(holder: BindingViewHolder, position: Int) {
       val repo = getItem(position)
       repo?.let {
           val binding = holder.binding as MovieItemBinding
           binding.repo = it
       }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder {
        val binding = MovieItemBinding.inflate(LayoutInflater.from(context),parent,false)
        return BindingViewHolder(binding)
    }

}